package org.jeecg.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.sys.entity.SysMesUser;
import org.jeecg.sys.entity.SysMesUserRole;
import org.jeecg.sys.service.ISysMesUserRoleService;
import org.jeecg.sys.service.ISysMesUserService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: MES用户
 * @Author: LiCY
 * @Date:   2023-08-09
 * @Version: V1.0
 */
@Api(tags="MES用户")
@RestController
@RequestMapping("/sys/sysMesUser")
@Slf4j
public class SysMesUserController extends JeecgController<SysMesUser, ISysMesUserService> {
	@Autowired
	private ISysMesUserService sysMesUserService;
	 @Autowired
	 private ISysMesUserRoleService iSysMesUserRoleService;
	 @Autowired
	 private CommonAPI commonAPI;
	/**
	 * 分页列表查询666
	 *
	 * @param sysMesUser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "MES用户-分页列表查询")
	@ApiOperation(value="MES用户-分页列表查询", notes="MES用户-分页列表查询")
	@GetMapping(value = "/list")
	@DS("history")
	public Result<IPage<SysMesUser>> queryPageList(SysMesUser sysMesUser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysMesUser> queryWrapper = QueryGenerator.initQueryWrapper(sysMesUser, req.getParameterMap());
		Page<SysMesUser> page = new Page<SysMesUser>(pageNo, pageSize);
		IPage<SysMesUser> pageList = sysMesUserService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param sysMesUser
	 * @return
	 */
	@AutoLog(value = "MES用户-添加")
	@ApiOperation(value="MES用户-添加", notes="MES用户-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:sys_mes_user:add")
	@PostMapping(value = "/add")
	@DS("business")
	public Result<String> add(@RequestBody SysMesUser sysMesUser) {
		sysMesUserService.save(sysMesUser);
		//获取角色ID并插入
		String selectedroles = sysMesUser.getSelectedroles();
		for (String s : selectedroles.split(",")) {
			Integer counterValue = commonAPI.getCounterValue("Id_sys_user_role");
			SysMesUserRole sysMesUserRole = new SysMesUserRole();
			sysMesUserRole.setId(counterValue);
			sysMesUserRole.setUserId(sysMesUser.getUsername());
			sysMesUserRole.setRoleId(s);
			iSysMesUserRoleService.save(sysMesUserRole);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param sysMesUser
	 * @return
	 */
	@AutoLog(value = "MES用户-编辑")
	@ApiOperation(value="MES用户-编辑", notes="MES用户-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:sys_mes_user:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	@DS("business")
	public Result<String> edit(@RequestBody SysMesUser sysMesUser) {
		sysMesUserService.updateById(sysMesUser);
		iSysMesUserRoleService.remove(new QueryWrapper<SysMesUserRole>().eq("user_id",sysMesUser.getUsername()));
		String selectedroles = sysMesUser.getSelectedroles();
		for (String s : selectedroles.split(",")) {
			SysMesUserRole sysMesUserRole = new SysMesUserRole();
			Integer counterValue = commonAPI.getCounterValue("Id_sys_user_role");
			sysMesUserRole.setId(counterValue);
			sysMesUserRole.setUserId(sysMesUser.getUsername());
			sysMesUserRole.setRoleId(s);
			iSysMesUserRoleService.save(sysMesUserRole);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过选中对象删除
	 *
	 * @param sysMesUser
	 * @return
	 */
	@AutoLog(value = "MES用户-通过id删除")
	@ApiOperation(value="MES用户-通过id删除", notes="MES用户-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:sys_mes_user:delete")
	@RequestMapping(value = "/delete")
	public Result<String> delete(@RequestBody SysMesUser sysMesUser) {
		sysMesUserService.removeById(sysMesUser);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "MES用户-批量删除")
	@ApiOperation(value="MES用户-批量删除", notes="MES用户-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:sys_mes_user:deleteBatch")
	@RequestMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestBody List<SysMesUser> list) {
	    list.forEach(item->{
    			sysMesUserService.removeById(item);
    		});
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "MES用户-通过id查询")
	@ApiOperation(value="MES用户-通过id查询", notes="MES用户-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("history")
	public Result<SysMesUser> queryById(@RequestParam(name="id",required=true) String id) {
		SysMesUser sysMesUser = sysMesUserService.getById(id);
		if(sysMesUser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysMesUser);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysMesUser
    */
    //@RequiresPermissions("org.jeecg.modules.demo:sys_mes_user:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("history")
    public ModelAndView exportXls(HttpServletRequest request, SysMesUser sysMesUser) {
        return super.exportXls(request, sysMesUser, SysMesUser.class, "MES用户");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sys_mes_user:importExcel")
    @DS("business")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysMesUser.class);
    }

}
